import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Users from '../components/user/Users.vue'
import Rights from '../components/rights/Rights.vue'
import Roles from '../components/rights/Roles.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    redirect: '/Welcome',
    component: Home,
    children: [
      {
        path: '/Welcome',
        component: Welcome
      },
      {
        path: '/Users',
        component: Users
      },
      {
        path: '/Rights',
        component: Rights
      },
      {
        path: '/Roles',
        component: Roles
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

export default router

// 路由导航守卫控制访问权限
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  if (!window.sessionStorage.getItem('token')) return next('login')
  next()
})
